/*
Hold calendar events
 */
package OpenToday.PIMHelper;

import OpenToday.Tools.TimeUtils;
import java.util.Date;
import javax.microedition.pim.Event;
import javax.microedition.pim.PIMItem;
import OpenToday.Tools.ExceptionUtils;

/**
 * this class represents one PIM entrie (calendar)
 * @author thomas
 */
public class PhoneCalendarEvent {

    private Date m_eventStartTime;
    private Date m_eventEndTime;
    private String m_eventTitle;
    private int m_eventType; // all day event calculation

    /**
     *
     * @param event
     * @throws java.lang.Exception
     * gets
     * m_eventStartTime,
     * m_eventEndTime,
     * m_eventTitle: Summary od Note
     * m_eventType: all day event calculation
     * Open issues are:
     * -javadoc/javax/microedition/pim/Event.html
     * If START and END are the same this event is an all day event.
     * if START an END is 0, seems that date is before 1.1.1970 (Birthday of my father)
     * implementes is: an all day event starts at 00:00:00 and ends 23:59:00
     * 
     */
    public PhoneCalendarEvent(Event event) throws Exception {

        int[] fields = event.getFields(); // evaluare empty fields
        // m_eventStartTime
        m_eventStartTime = new Date(event.getDate(Event.START, PIMItem.ATTR_NONE));

        // m_eventEndTime
        if (PIMHelper.hasField(fields, Event.END)) {
            m_eventEndTime = new Date(event.getDate(Event.END, PIMItem.ATTR_NONE));
        } else {
            m_eventEndTime = new Date();
            m_eventEndTime.setTime(m_eventStartTime.getTime() + 1);
        }

        // m_eventTitle
        if (PIMHelper.hasField(fields, Event.SUMMARY))//summery is the best
        {
            m_eventTitle = event.getString(Event.SUMMARY, PIMItem.ATTR_NONE);
        } else {//if we do not have a summary, we'll try the NOTE
            if (PIMHelper.hasField(fields, Event.NOTE)) {
                m_eventTitle = event.getString(Event.NOTE, PIMItem.ATTR_NONE);
            } else//Nothing?! what now?
            {
                m_eventTitle = "";
            }
        }


        if (PIMHelper.hasField(fields, Event.EXTENDED_FIELD_MIN_VALUE)) {
            m_eventType = event.getInt(Event.EXTENDED_FIELD_MIN_VALUE, PIMItem.ATTR_NONE);
        } else //workaround for my phone (k610i)
        //an all day event starts at 00:00:00 and ends 23:59:00 this are 86340000 seconds (86339000)
        if ((m_eventEndTime.getTime() - m_eventStartTime.getTime()) == 86340000) {
            m_eventType = 2;
        } else {
            m_eventType = 0;
        }
    }

    public Date getEventStartTime() {
        return m_eventStartTime;
    }

    public Date getEventEndTime() {
        return m_eventEndTime;
    }

    public String getEventTitle() {
        return m_eventTitle;
    }

    public boolean isAllDayEvent() {
        return m_eventType == 2;
    }

    /**
     * Event ocures at this time
     * only time is relevant, not the date
     * @return
     */
    public boolean isCurrentEvent() {
        if (isAllDayEvent()) {
            return false;//all day events are never the current
        } else {

            long currentTime = (new Date()).getTime();

            // we like to compare the time during the day
            // reoccuring events have a creation date as time string
            // therefor there will not become curent on a later accurence
            long daystart = TimeUtils.getDayStart(currentTime);
            long n_start = m_eventStartTime.getTime() - daystart;
            long n_end = m_eventEndTime.getTime() - daystart;
            long n_current = currentTime - daystart;

            //ExceptionUtils.PHASE("isCurrentEvent currentTime      " + new Date(currentTime));
            //ExceptionUtils.PHASE("isCurrentEvent m_eventStartTime " + m_eventStartTime);
            //ExceptionUtils.PHASE("isCurrentEvent m_eventEndTime   " + m_eventEndTime);

            //for all appointments on the next day the n_start or end is negative
            //return false because they are not current enyway
            if (n_start >= 0 || n_end >= 0) {
                return ((n_current >= n_start) && (n_current < n_end));
            } else {
                return false;
            }
        //return ((currentTime >= m_eventStartTime.getTime()) && (currentTime < m_eventEndTime.getTime()));
        }
    }

    /**
     * calculate day index as int
     * is wrong it uses the event creation date --> reoccuring events
     * @return day as an index
     */
    public int getDayIndex() {
        long currentTime = (new Date()).getTime();
        long day = TimeUtils.CalculateDaysDiff(currentTime, m_eventStartTime.getTime());
        return (int) day;
    }


}
